/**
 * @Copyright 
 * @Project inetdisk
 * @Package wxyz.inetdisk.util
 * @File CheckLoginFilter.java
 * @CreateTime 2015-10-23 上午8:55:41
 */
package ren.wxyz.inetdisk.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;

import ren.wxyz.inetdisk.util.Constants;
import ren.wxyz.inetdisk.util.DigitUtils;
import ren.wxyz.inetdisk.util.UrlPattern;
import ren.wxyz.inetdisk.vo.RespMsg;


/**
 * <p>
 * 登陆检查器
 * </p>
 * @Author wxyz
 * @CreateTime 2015-10-23 上午8:55:41 init class
 */
public class CheckLoginFilter implements Filter {

	/**
	 * 静态资源列表
	 */
	private List<UrlPattern> notFilterUrl = new ArrayList<UrlPattern>();
	
	public CheckLoginFilter() {
		// 添加默认的静态资源
		addStaticResource(new String[] {
				"*.html",
				"*.htm",
				"*.css",
				"*.js",
				"*.ico",
				"*.png",
				"*.jpg"
				});
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest hsreq = (HttpServletRequest) req;
		HttpServletResponse hsresp = (HttpServletResponse) resp;
		
		if (!checkStaticResource(hsreq)) {
			HttpSession session = hsreq.getSession();
			if (null == session || session.getAttribute(Constants.SESSION_KEY) == null) {
				String respMsg = DigitUtils.toJson(new RespMsg<>(Constants.MSG_NOLOGIN, "没有登录"));
				
				hsresp.setHeader("content-type", "application/json; charset=UTF-8");
				hsresp.getWriter().write(respMsg);
				return ;
			}
		}
		
		chain.doFilter(hsreq, hsresp);
	}
	
	@Override
	public void init(FilterConfig cfg) throws ServletException {
		// 读取配置项
		String statics = cfg.getInitParameter("statics");
		
		// 解析参数并添加到静态资源url集合
		if (!StringUtils.isBlank(statics)) {
			addStaticResource(statics.split(";"));
		}
	}

	@Override
	public void destroy() {
	}
	
	/**
	 * <p>
	 * 将静态资源加入本地集合中
	 * </p>
	 * @param statics 静态资源列表
	 * @CreateTime 2015-10-23 上午9:18:38 create method
	 */
	private void addStaticResource(String... statics) {
		if (null == statics || statics.length == 0) {
			return ;
		}
		
		// 添加URL到静态资源集合
		for (String stt : statics) {
			// 原有的资源集合中没有，则加入
			if (!notFilterUrl.contains(stt)) {
				notFilterUrl.add(new UrlPattern(stt));
			}
		}
	}
	
	/**
	 * <p>
	 * 检查URL是否是在不匹配的模型
	 * </p>
	 * @param req 请求对象
	 * @return
	 * @CreateTime 2015-10-23 上午9:25:27 create method
	 */
	private boolean checkStaticResource(HttpServletRequest req) {
		String url = req.getServletPath() + (req.getPathInfo() == null 
				? "" : req.getPathInfo());
		
		if (url.equals("/")) {
			return true;
		}
		
		for (UrlPattern ptn : notFilterUrl) {
			// 检查到了立即返回
			if (ptn.match(url)) {
				return true;
			}
		}
		
		return false;
	}

}
